$(function () {
	// body...

    localStorage.indexName = titleIndex;
	
	function productsFun(){

		var productsNav = $('#productsNav');
		var qieStyle = $('#qieStyle');
		var blockBut = qieStyle.find('.block');
		var listBut = qieStyle.find('.list');
		var productsList =$('#productsList');
		blockBut.click(function(){
			$(this).addClass('active');
			listBut.removeClass('active');
			productsList.removeClass('styleTwo').addClass('styleOne');
		});
		listBut.click(function(){
			$(this).addClass('active');
			blockBut.removeClass('active');
			productsList.removeClass('styleOne').addClass('styleTwo');
		})

	}
	productsFun()


	function featuredSweiper(){
		MySweiper = new Swiper ('#featuredSweiper', {
	    // direction: 'vertical',
	    loop: true,
	    // 如果需要分页器
	    pagination: '.swiper-pagination',
	    paginationClickable :true,
	    
	    
	    // 如果需要滚动条
	     // scrollbar: '.swiper-scrollbar',


	 })  



	}
	// featuredSweiper()

	function SweiperBom(){
		MySweiper = new Swiper ('#SweiperBom', {
	    // direction: 'vertical',
	    loop: true,
	    slidesPerView : 2.99,
	    // 如果需要分页器
	    // 如果需要前进后退按钮
	    nextButton: '.swiper-button-next',
	    prevButton: '.swiper-button-prev',


	})  



	}
	// SweiperBom()



	
	$.ajax({
        url: 'http://47.89.20.150/czlycms/index.php?m=Web&a=product',
        type: 'GET',
        dataType: 'JSONP'
    })
    .done(function(data) {
        console.log("success");
        console.log(data);
        product_slidesFun(data.product_slides);
        our_productsFun(data.our_products.products);
        listKutitle(data.our_products.title);
        listKuseo_keywords(data.our_products.seo_keywords);
        best_sellresFun(data.best_sellres);
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });





// product_slides
function product_slidesFun(data){
	var featuredCon = $('#featuredCon');
	var html = '';

	for (var i = 0; i < data.length; i++) {
		var cn = '<div class="swiper-slide">'+
					'<div class="centerCon" >'+
						'<div class="head">'+data[i].title+'</div>'+
						'<div class="shopList">'+forNei(data[i].products)+'</div>'+
						'<div class="imgC"><img src="'+data[i].image_url+'" alt=""></div>'+
					'</div>'+
				'</div>';
		html+=cn;
	}
	featuredCon.html(html);
	featuredSweiper();
	function forNei(list){
		var htm = ''
		for (var i = 0; i < list.length; i++) {
			var cn = '<a href="article.html?article_id='+list[i].id+'"><img src="'+list[i].image_url+'" alt=""></a>';
			htm+=cn;
		}
		return htm;
	}
}
// product_slides end




// best_sellres
	function best_sellresFun(data){
	var SweiperBomCn = $('#SweiperBomCn');
		var html = '';

		for (var i = 0; i < data.length; i++) {
			var cn = '<div class="swiper-slide">'+
							'<div class="slideNei">'+
								'<a href="article.html?article_id='+data[i].article_id+'">'+
									'<div class="imgC"><img src="'+data[i].image_url+'"></div>'+
									'<div class="word">'+
										'<h4>'+data[i].title+'</h4>'+
									'</div>'+
								'</a>'+
							'</div>'+
						'</div>';
			html+=cn;
		}
		SweiperBomCn.html(html);
		SweiperBom();
	}
// best_sellres end



// our_productsFun
function our_productsFun(data){
	var productsList = $('#productsList');
		var html = '';

		for (var i = 0; i < data.length; i++) {
			var cn = '<li class="clearfloat">'+
						'<a href="article.html?article_id='+data[i].article_id+'">'+
							'<div class="imgC"><img src="'+data[i].image_url+'"></div>'+
							'<div class="rig">'+
								'<div class="title" title="'+data[i].title+'">'+data[i].title+'</div>'+
								'<div class="author">'+data[i].seo_keywords+'</div>'+
								'<p>'+data[i].seo_description+'</p>'+
							'</div>'+
						'</a>'+
					'</li>';
			html+=cn;
		}
		
		productsList.html(html);
		SweiperBom();
}

// our_products end



// listKutitle
function listKutitle(data){
	var con = $('#listKutitle');
	var html = '';
	for (var i = 0; i < data.length; i++) {
		var cn  = '<li>'+data[i].title+'</li>';
		html+=cn;
	}
	$('#Indtitle').val(data[0].title)
	con.html(html);
}
// listKutitle end



// listKuseo_keywords
function listKuseo_keywords(data){
	var con = $('#listKuseo_keywords');
	var html = '';
	for (var i = 0; i < data.length; i++) {
		var cn  = '<li>'+data[i].seo_keywords+'</li>';
		html+=cn;
	}
	$('#Indkeywords').val(data[0].seo_keywords)
	con.html(html);
}
// listKuseo_keywords end


















//收索控制
	BomListFun()

BomListFunS()

function BomListFun(){
	var BomListFun = $('#BomList');
	var Lone = BomListFun.find('.Lone');
	var Indtitle = BomListFun.find('#Indtitle');
	var Indkeywords = BomListFun.find('#Indkeywords');
	var listKu = Lone.find('#listKutitle');


	Indtitle.focus(function(){
		Lone.addClass('active');
	})

	listKu.on('click','li',function(){
		 Indtitle.val($(this).text());
		 Indtitle.blur();
		 Lone.removeClass('active');
		 var search_name = Indtitle.val();
		Search('title',search_name)

	})
	// Lone.mouseout(function(){
	// 	Lone.removeClass('active');
	// })

}
function BomListFunS(){
	var BomListFun = $('#BomList');
	var Lone = BomListFun.find('.Ltwo');

	var Indtitle = BomListFun.find('#Indtitle');
	var Indkeywords = BomListFun.find('#Indkeywords');
	var listKu = Lone.find('#listKuseo_keywords');
	

	Indkeywords.focus(function(){
		Lone.addClass('active');
	})

	listKu.on('click','li',function(){
		 Indkeywords.val($(this).text());
		 Indkeywords.blur();
		 Lone.removeClass('active');
		var search_type = Indkeywords.val();
		Search('name',search_type)


	})
	// Lone.mouseout(function(){
	// 	Lone.removeClass('active');
	// })  

}




function SearchFun(){
	var form = $('div.Search');
	var input = form.find('input');
	var but = form.find('div.iconC');


	but.click(function(){
		Search('seo_keywords',input.val());
	})

	input.focus(function(){
		input.bind('keydown',function(event){
			console.log(event.keyCode);
			if (event.keyCode == 13) {
				var ind = input.val()
				if (ind.length != 0) {
					Search('seo_keywords',ind);
				}
			}
		})
	});

	input.blur(function(){
		input.unbind('keydown')
	})
}

SearchFun()











function Search(title,search_name){
	console.log(title);
	console.log(search_name);
	

	$.ajax({
        url: 'http://47.89.20.150/czlycms/index.php?m=Web&a=product_search&search_type='+title+'&search_name='+search_name,
        type: 'GET',
        dataType: 'JSONP'
    })
    .done(function(data) {
    	console.log(data);
      our_productsFun(data.products)
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });
}
//收索控制 end












})